package com.lab.web.controller.biz;

import com.lab.common.core.controller.BaseController;
import com.lab.common.core.domain.AjaxResult;
import com.lab.common.core.domain.entity.SysRole;
import com.lab.common.core.domain.model.LoginUser;
import com.lab.common.core.dto.AuditDTO;
import com.lab.common.enums.AuditEnum;
import com.lab.system.domain.LabBorrow;
import com.lab.system.domain.Reservations;
import com.lab.system.service.ILabBorrowService;
import com.lab.system.service.IReservationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/system/audit")
@RestController
public class DashBoardController extends BaseController {

    @Autowired
    private ILabBorrowService labBorrowService;

    @Autowired
    private IReservationsService reservationsService;

    /**
     * 获取所有等待审批的预约记录
     */
    @GetMapping("/list")
    public AjaxResult list() {
        // 获取当前身份的角色，根据角色来判断审批内容
        // 超级管理员可以审批，一级，二级审批
        // 学院管理员审批二级审批
        // 指导教师审批一级审批
        LoginUser loginUser = getLoginUser();
        Long[] roleIds = loginUser.getUser().getRoles().stream().map(SysRole::getRoleId).toArray(Long[]::new);
        for (Long roleId : roleIds) {
            if (roleId == 1L) {
                // 超级管理员
                List<LabBorrow> labBorrows1 = labBorrowService.getAllAudit(AuditEnum.FIRST_AUDIT.getCode());
                List<LabBorrow> labBorrows2 = labBorrowService.getAllAudit(AuditEnum.AUDITING.getCode());
                labBorrows1.addAll(labBorrows2);
                List<Reservations> reservationsList1 = reservationsService.getAllAudit(AuditEnum.FIRST_AUDIT.getCode());
                List<Reservations> reservationsList2 = reservationsService.getAllAudit(AuditEnum.AUDITING.getCode());
                reservationsList1.addAll(reservationsList2);
                // 聚合转换为AuditDTO
                List<AuditDTO> auditDTOS = mergeDTO(labBorrows1,reservationsList1);
                return AjaxResult.success(auditDTOS);
            } else if (roleId == 101L) {
                // 学院管理员
                List<LabBorrow> labBorrows1 = labBorrowService.getAllAudit(AuditEnum.FIRST_AUDIT.getCode());
                List<Reservations> reservationsList1 = reservationsService.getAllAudit(AuditEnum.FIRST_AUDIT.getCode());
                // 聚合转换为AuditDTO
                List<AuditDTO> auditDTOS = mergeDTO(labBorrows1,reservationsList1);
                return AjaxResult.success(auditDTOS);
            } else if (roleId == 102L) {
                // 指导教师
                labBorrowService.getAllAudit(AuditEnum.AUDITING.getCode());
                List<LabBorrow> labBorrows2 = labBorrowService.getAllAudit(AuditEnum.AUDITING.getCode());
                List<Reservations> reservationsList2 = reservationsService.getAllAudit(AuditEnum.AUDITING.getCode());
                // 聚合转换为AuditDTO
                List<AuditDTO> auditDTOS = mergeDTO(labBorrows2,reservationsList2);
                return AjaxResult.success(auditDTOS);
            }
        }
        return AjaxResult.success();
    }

    /**
     * 查询看板数据
     */
    @GetMapping("/getDashBoardData")
    public AjaxResult getDashBoardData() {
        // 本周实验室预约趋势

        // 本周实验室使用率

        // 本周实验课程类型分布

        // 预约时段分布

        // 器材租用前10位
        return null;
    }


    private List<AuditDTO> mergeDTO(List<LabBorrow> labBorrows, List<Reservations> reservationsList) {
        List<AuditDTO> auditDTOs = new ArrayList<>();
        for (LabBorrow labBorrow : labBorrows) {
            AuditDTO auditDTO = new AuditDTO();
            auditDTO.setId(labBorrow.getId());
            auditDTO.setApplyTime(labBorrow.getStartTime());
            auditDTO.setStatus(labBorrow.getAudit());
            // 1-实验室预约
            auditDTO.setAuditTypeDesc("实验室预约");
            auditDTO.setAuditType(1);
            auditDTO.setStatusDesc(AuditEnum.getLabStatus(labBorrow.getAudit()).getInfo());
            auditDTO.setUserName(labBorrow.getUserName());
            auditDTOs.add(auditDTO);
        }

        for (Reservations reservations : reservationsList) {
            AuditDTO auditDTO = new AuditDTO();
            auditDTO.setId(reservations.getId());
            auditDTO.setApplyTime(reservations.getStartTime());
            auditDTO.setStatus(reservations.getAudit());
            auditDTO.setStatusDesc(AuditEnum.getLabStatus(reservations.getAudit()).getInfo());
            auditDTO.setAuditTypeDesc("实验课程预约");
            auditDTO.setAuditType(2);
            auditDTO.setUserName(reservations.getUserName());
            auditDTOs.add(auditDTO);
        }
        return auditDTOs;
    }

}
